package com.pig4cloud.util;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.Map;

/**
 * @Description: 公共工具类
 * @CreateTime: 2023-12-08
 * @Author: Oliver
 * @Version: 1.0.0
 */
public class CommonUtils {

	public static final String REPEAT_PARAMS = "repeatParams";

	public static final String REPEAT_TIME = "repeatTime";

	public static final String REPEAT_SUBMIT_KEY = "repeat_submit:";

	/**
	 * 判断参数是否相同
	 */
	public static boolean compareParams(Map<String, Object> nowMap, Map<String, Object> preMap) {
		String nowParams = (String) nowMap.get(REPEAT_PARAMS);
		String preParams = (String) preMap.get(REPEAT_PARAMS);
		return nowParams.equals(preParams);
	}

	/**
	 * 判断两次间隔时间
	 */
	public static boolean compareTime(Map<String, Object> nowMap, Map<String, Object> preMap, int interval) {
		long time1 = (Long) nowMap.get(REPEAT_TIME);
		long time2 = (Long) preMap.get(REPEAT_TIME);
		return (time1 - time2) < interval;
	}

	public static String toJsonString(Object object) {
		if (object != null) {
			try {
				return new ObjectMapper().writeValueAsString(object);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}
}
